package com.axx.android.profile;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;

import com.axx.android.LetterApp;
import com.axx.android.R;
import com.axx.android.base.BaseActivity;
import com.axx.android.common.BaseModel;
import com.axx.android.common.Constants;
import com.axx.android.utils.GsonUtils;
import com.axx.android.utils.HttpUtils;
import com.google.gson.reflect.TypeToken;

import org.json.JSONException;

import java.util.HashMap;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SendFriendActivity extends BaseActivity {

    @Bind(R.id.btn_back)
    Button btnBack;
    @Bind(R.id.btn_reg)
    Button btnReg;
    @Bind(R.id.layout_toolbar)
    RelativeLayout layoutToolbar;
    @Bind(R.id.et_name)
    EditText etName;
    @Bind(R.id.et_phone)
    EditText etPhone;
    @Bind(R.id.et_content)
    EditText etContent;


    @Override
    public int getLayoutId() {
        return R.layout.activity_send_friend;
    }

    @Override
    public void init(Bundle savedInstanceState) {

    }

    @OnClick(R.id.btn_reg)
    public void onViewClicked() {
        if (TextUtils.isEmpty(etName.getText().toString())) {
            showToast("请输入姓名");
            return;
        }
        if (TextUtils.isEmpty(etPhone.getText().toString())) {
            showToast("请输入电话号");
            return;
        }
        if (TextUtils.isEmpty(etContent.getText().toString())) {
            showToast("请输入内容");
            return;
        }
        showLoading("");

        HashMap<String, String> map = new HashMap<>();
        map.put("userId", LetterApp.authUser.getId());
        map.put("my_real_name", LetterApp.authUser.getRealName());
        map.put("phone", etPhone.getText().toString());
        map.put("real_name", etName.getText().toString());
        map.put("title", etContent.getText().toString());
        map.put("text", etContent.getText().toString());
        HttpUtils.post(Constants.Url.User.USER_SEND_FRIEND, map, new HttpUtils.SJCallBack() {
            @Override
            public void onSuccess(String result) {
                hideLoading();
                try {
                    BaseModel baseModel = GsonUtils.fromJson(result, new TypeToken<BaseModel>() {
                    });
                    if (baseModel.isSuccess()) {
                        showToast("好友邀请成功");
                        etPhone.setText("");
                        etName.setText("");
                        etContent.setText("");
                    } else {
                        showToast(baseModel.message);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                    showErrorParse();
                }

            }

            @Override
            public void onError() {
                hideLoading();
                showErrorNet();
            }
        });
    }
}
